package com.github.binarywang.demo.wx.mp.controller;

import com.github.binarywang.demo.wx.mp.beans.AdvertMpData;
import com.github.binarywang.demo.wx.mp.beans.MeiPaParam;
import com.github.binarywang.demo.wx.mp.beans.WxMpUser;
import com.github.binarywang.demo.wx.mp.dto.Result;
import com.github.binarywang.demo.wx.mp.service.*;
import com.github.binarywang.demo.wx.mp.utils.Md5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "号主推广模块")
@RestController
@RequestMapping
@Slf4j
@AllArgsConstructor
public class AdvertApiCTO {
    private final QrService qrService;
    private final LsWxMpUserService lsWxMpUserService;
    private final FocusInfoService focusInfoService;
    private final RedisService redisService;

    private final wxuserservice ws;
    private final masterservice ms;


    @ApiOperation("获得推广")
    @PostMapping("/advert/api/get")
    public Result<AdvertMpData> getAdvertMpData(@RequestBody MeiPaParam param){// 性别 nickname 国家
        String data = param.getApiid();
        data += param.getNickname();
        data += param.getScene();
        String onece = Md5Utils.hash(data);
        if (redisService.get("yunfen:onece:"+onece)!=null){
            log.info("短时重复请求....");
            return Result.error("短时重复请求...");
        }
        log.debug("商家验证没有做噢~~~");
//        try{
//            WxMpUser wxMpUser = new WxMpUser();
////            wxMpUser.setHeadimgUrl(param.getHeadimgurl());
//            wxMpUser.setNickname(param.getNickname());
//            String sex = param.getSex();
//            if (sex!=null){
////                Integer s = Integer.parseInt(sex);
//                wxMpUser.setSex(sex);
//            }
//            //性别先不管
//            if (StringUtils.isNotEmpty(param.getProvince())){
//                wxMpUser.setProvince(param.getProvince());
//            }
//            if (StringUtils.isNotEmpty(param.getCity())){
//                wxMpUser.setProvince(param.getCity());
//            }
//            if (StringUtils.isNotEmpty(param.getCountry())){
//                wxMpUser.setProvince(param.getCountry());
//            }
//            if (StringUtils.isNotEmpty(param.getProvince())){
//                wxMpUser.setProvince(param.getProvince());
//            }
//            WxMpUser info = lsWxMpUserService.getByBasicInfo(wxMpUser);//getByBasicInfo中的findone有问题
//            log.info("查询该用户存不存在 -> {}",info);


        try {
            String nickname = param.getNickname();
            String sex = param.getSex();
            String country = param.getCountry();
            String province = param.getProvince();
            String masterid=param.getMasterid();
            Boolean on = ms.findById(masterid);

            List<WxMpUser> info = ws.findUserByparam(nickname, sex, country,province);//查看该用户是否在关注user表中

            log.info("查询该用户存不存在 -> {}",info);
            redisService.set("yunfen:onece:"+onece,1,60);
            return focusInfoService.get(info, param, qrService);//AdvertMpData ad    log.info("拿到的广告 {}",ad);

        }catch(Exception e){
            return Result.error("错误");
        }
    }

}
